package com.leetcode.No1239;

import org.junit.Test;

import java.util.*;

/**
 * @program: leetcode
 * @description: 串联字符串的最大长度
 * @author: wangzhihua
 * @date: 2021-06-20
 */
public class Solution01 {

	@Test
	public void test() {
		List<String> strings = Arrays.asList("abc", "b");
		System.out.println(maxLength(strings));
	}

	private List<String> arr;
	private int arrSize;
	private int maxLen;

	public int maxLength(List<String> arr) {
		this.arr = arr;
		arrSize = arr.size();
		maxLen = 0;
		selection(0, "");
		return maxLen;
	}

	private void selection(int index, String currentStr) {
		if (index == this.arr.size()) {
			maxLen = containSameChar(currentStr) ? maxLen : Math.max(maxLen, currentStr.length());
			return;
		}
		selection(index + 1, currentStr);
		selection(index + 1, currentStr + arr.get(index));
	}

	// true：含有相同字符；false：不含有相同字符
	private boolean containSameChar(String str) {
		HashSet<Character> charSet = new HashSet<Character>();
		for (int i = 0; i < str.length(); ++i) {
			charSet.add(str.charAt(i));
		}
		return charSet.size() != str.length();
	}

}
